package com.lagou.util;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lagou.service.UserService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class ServiceCache implements ApplicationContextAware {


    public static Map<String, UserService> services = Maps.newHashMap();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, UserService> beans = applicationContext.getBeansOfType(UserService.class);
        if (beans.size() == 1) {
            services.put(UserService.class.getName(), Lists.newArrayList(beans.entrySet()).get(0).getValue());
        }


    }
}

